# api/views.py
from rest_framework import views, status
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.response import Response
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import AllowAny
from api.serializers import OllamaQuestionSerializer, UserSerializer, TokenSerializer
from api.models import OllamaQuestion

class OllamaQuestionView(views.APIView):
    serializer_class = OllamaQuestionSerializer
    authentication_classes = [TokenAuthentication]

    def get(self, request, format=None):
        qs = OllamaQuestion.objects.all()
        serializer = self.serializer_class(qs, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

# 用户和 token 视图 (稍后定义)
class UserView(views.APIView):
    serializer_class = UserSerializer
    permission_classes = [AllowAny]

class TokenView(ObtainAuthToken):
    serializer_class = TokenSerializer
